<?php
	class Manage_channel extends CI_Controller{	
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_channel");
			$this->load->library("pagination");
		}
		
		public function index(){
			//== template
			redirect('manage_channel/channellist');
		}
		
		public function channellist($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($item,$num,$page,$total) = $this->db_channel->listAll();
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("item",$item);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			//== template
			$mark  = 4;
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu($mark));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_channel/channel_item_delAll"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/channel/channel-list.html');
		}
		
		public function channel_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("name","name","required");
				$this->form_validation->set_rules("link","link","required");
				$this->form_validation->set_rules("android_link","link android","required");
				$this->form_validation->set_rules("tv_class","tv class","required");
				$this->form_validation->set_rules("tv_lang","Language","required");
				$this->form_validation->set_rules("tv_country","Country","required");
			
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("link",set_value("link"));
					$this->cismarty->assign("android_link",set_value("android_link"));
					//$this->cismarty->assign("tv_class",set_value("tv_class"));
			
					//set error
					
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("errorlink",form_error('link'));
					$this->cismarty->assign("errorandroidlink",form_error('android_link'));
					$this->cismarty->assign("errorclass",form_error('tv_class'));
					$this->cismarty->assign("errorlang",form_error('tv_lang'));
					$this->cismarty->assign("errorcountry",form_error('tv_country'));
					$this->cismarty->assign("errorstatus",form_error('status'));
				}else {
					$error = false;
					$picname = "";
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/banner/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 100;
							$config['height'] = 70;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"TVName"=>trim($this->input->post('name')),
								"TVLink"=>trim($this->input->post('link')),
								"TVLinkIOS"=>trim($this->input->post('android_link')),
								"TVClass"=>trim($this->input->post("tv_class")),
								"TVLanguage"=>trim($this->input->post("tv_lang")),
								"TVArea"=>trim($this->input->post("tv_country")),
								"pic_path"=>$picname,
								"status"=>$this->input->post('status')
						);
						$res = $this->db->insert('mobile_p_tv_info',$data);
						if($res) redirect('manage_channel/channellist/success');
						else  redirect('manage_channel/channellist/fail');
					}
				}
			}//end post
			$this->cismarty->assign("strclass",$this->area());
			$this->cismarty->assign("strcountry",$this->country());
			$this->cismarty->assign("strlang",$this->lang());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(4));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart('manage_channel/channel_add'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/channel/channel_add.html');
			
		}
		
		public function channel_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			$class ="";
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("name","name","required");
				$this->form_validation->set_rules("link","link","trim");
				$this->form_validation->set_rules("android_link","link android","trim");
				$this->form_validation->set_rules("tv_class","tv class","required");
				$this->form_validation->set_rules("tv_lang","Language","required");
				$this->form_validation->set_rules("tv_country","Country","required");
			
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("link",set_value("link"));
					$this->cismarty->assign("android_link",set_value("android_link"));
					//$this->cismarty->assign("tv_class",set_value("tv_class"));
			
					//set error
					
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("errorlink",form_error('link'));
					$this->cismarty->assign("errorandroidlink",form_error('android_link'));
					$this->cismarty->assign("errorclass",form_error('tv_class'));
					$this->cismarty->assign("errorlang",form_error('tv_lang'));
					$this->cismarty->assign("errorcountry",form_error('tv_country'));
					$this->cismarty->assign("errorstatus",form_error('status'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/banner/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 100;
							$config['height'] = 70;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$uppic = array("pic_path"=>$picname);
							$this->db->where("TVID",$id);
							$this->db->update('mobile_p_tv_info',$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"TVName"=>trim($this->input->post('name')),
								"TVLink"=>trim($this->input->post('link')),
								"TVLinkIOS"=>trim($this->input->post('android_link')),
								"TVClass"=>trim($this->input->post("tv_class")),
								"TVLanguage"=>trim($this->input->post("tv_lang")),
								"TVArea"=>trim($this->input->post("tv_country")),
								"status"=>$this->input->post('status')
						);
						$this->db->where("TVID",$id);
						$res = $this->db->update('mobile_p_tv_info',$data);
						if($res) redirect('manage_channel/channellist/success');
						else  redirect('manage_channel/channellist/fail');
					}
				}
			}//end post
			else{
				$this->db->where("TVID",$id);
				$row = $this->db->get("mobile_p_tv_info")->row();
				$this->cismarty->assign("name",$row->TVName);
				$this->cismarty->assign("link",$row->TVLink);
				$this->cismarty->assign("android_link",$row->TVLinkIOS);
				$class = $row->TVClass;
				$lang	 = $row->TVLanguage;
				$country = $row->TVArea;
				if(trim($row->pic_path) != ""){
					$this->cismarty->assign("img",'<img src="'.base_url().'media/banner/'.$row->pic_path.'"><br>');
				}
				if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
				else $this->cismarty->assign("sd",'checked="checked"');
			}
			$this->cismarty->assign("strclass",$this->area($class));	
			$this->cismarty->assign("strcountry",$this->country($country));
			$this->cismarty->assign("strlang",$this->lang($lang));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(4));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_channel/channel_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/channel/channel_edit.html');
			
		}
		
	public function channel_item_del($id){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($id != ""){
 			$this->db->where('TVID',$id);
 			$res = $this->db->delete("mobile_p_tv_info");
 			if($res) redirect('manage_channel/channellist/success');
 		}
 	}
 	
 	public function channel_item_delAll(){
 		$check = $this->input->post('check');
 		if($check != ""){
 			foreach ($check as $index){
 				$this->db->where('TVID',$index);
 				$res = $this->db->delete("mobile_p_tv_info");
 			}
 		}
 		redirect('manage_channel/channellist/success');
 	}
		
		private function area($id=""){
			$this->db->order_by("pkid","asc");
			$res = $this->db->get("mobile_p_tv_class")->result_array();
			$str = '<option value="">=select tv class=</option>';
			foreach ($res as $P){
				if($id==$P['pkid']) $check ="selected";
				else $check = "";
				$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name_en'].'</option>';
			}
			return $str;
		}
		
		private function country($id=""){
			$this->db->order_by("pkid","asc");
			$res = $this->db->get("mobile_p_tv_country")->result_array();
			$str = '<option value="">=select country=</option>';
			foreach ($res as $P){
				if($id==$P['pkid']) $check ="selected";
				else $check = "";
				$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name_en'].'</option>';
			}
			return $str;
		}
		
		private function lang($id=""){
			$this->db->order_by("pkid","asc");
			$res = $this->db->get("mobile_p_tv_language")->result_array();
			$str = '<option value="">=select language=</option>';
			foreach ($res as $P){
				if($id==$P['pkid']) $check ="selected";
				else $check = "";
				$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name_en'].'</option>';
			}
			return $str;
		}
	}
?>